<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="theme-color" content="#0078E7"><meta name="author" content="爱肖彤真是太好了"><meta name="copyright" content="爱肖彤真是太好了"><meta name="generator" content="Hexo 5.1.1"><meta name="theme" content="hexo-theme-yun"><title>celery基础 | 工藤旧二の博客</title><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@900&amp;display=swap" media="none" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/star-markdown-css@0.1.19/dist/yun/yun-markdown.min.css"><script src="//at.alicdn.com/t/font_1140697_stqaphw3j4.js" async></script><script src="https://cdn.jsdelivr.net/npm/scrollreveal/dist/scrollreveal.min.js" defer></script><script>document.addEventListener("DOMContentLoaded", () => {
  [".post-card",".post-content img"].forEach((target)=> {
    ScrollReveal().reveal(target);
  })
});
</script><link rel="shortcut icon" type="image/svg+xml" href="/task/yun.ico"><link rel="mask-icon" href="/task/yun.ico" color="#0078E7"><link rel="alternate icon" href="/yun.ico"><link rel="preload" href="/task/css/hexo-theme-yun.css" as="style"><link rel="preload" href="/task/js/utils.js" as="script"><link rel="preload" href="/task/js/hexo-theme-yun.js" as="script"><link rel="prefetch" href="/task/js/sidebar.js" as="script"><link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin><link rel="stylesheet" href="/task/css/hexo-theme-yun.css"><link rel="alternate" href="/task/atom.xml" title="工藤旧二の博客" type="application/atom+xml"><script id="yun-config">
    const Yun = window.Yun || {};
    window.CONFIG = {"root":"/task/","title":"工藤旧二の博客","version":"0.9.7","anonymous_image":"https://cdn.jsdelivr.net/gh/YunYouJun/cdn/img/avatar/none.jpg","say":{"api":"https://v1.hitokoto.cn","hitokoto":true},"local_search":{"path":"/task/search.xml"},"fireworks":{"colors":["102, 167, 221","62, 131, 225","33, 78, 194"]}};
  </script><script>(function(){
  var bp = document.createElement('script');
  var curProtocol = window.location.protocol.split(':')[0];
  if (curProtocol === 'https') {
    bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
  }
  else {
    bp.src = 'http://push.zhanzhang.baidu.com/push.js';
  }
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(bp, s);
})();</script><meta name="description" content="一、celery介绍Celery 官网：http:&#x2F;&#x2F;www.celeryproject.org&#x2F; Celery 官方文档英文版：http:&#x2F;&#x2F;docs.celeryproject.org&#x2F;en&#x2F;latest&#x2F;index.html Celery 官方文档中文版：http:&#x2F;&#x2F;docs.jinkan.org&#x2F;docs&#x2F;celery&#x2F;  异步任务框架 执行异步任务 执行延迟任务 执行定时任务  二、c">
<meta property="og:type" content="article">
<meta property="og:title" content="celery基础">
<meta property="og:url" content="http://huang_zhao.gitee.io/task/2020/04/25/python/%E6%A8%A1%E5%9D%97/celery/index.html">
<meta property="og:site_name" content="工藤旧二の博客">
<meta property="og:description" content="一、celery介绍Celery 官网：http:&#x2F;&#x2F;www.celeryproject.org&#x2F; Celery 官方文档英文版：http:&#x2F;&#x2F;docs.celeryproject.org&#x2F;en&#x2F;latest&#x2F;index.html Celery 官方文档中文版：http:&#x2F;&#x2F;docs.jinkan.org&#x2F;docs&#x2F;celery&#x2F;  异步任务框架 执行异步任务 执行延迟任务 执行定时任务  二、c">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/huang_zhao/hz/raw/master/hz/007S8ZIlgy1ggqw0f7bltj30q80c174d.jpg">
<meta property="article:published_time" content="2020-04-25T05:20:11.000Z">
<meta property="article:modified_time" content="2020-08-26T01:02:17.000Z">
<meta property="article:author" content="爱肖彤真是太好了">
<meta property="article:tag" content="django">
<meta property="article:tag" content="python">
<meta property="article:tag" content="异步任务框架">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/huang_zhao/hz/raw/master/hz/007S8ZIlgy1ggqw0f7bltj30q80c174d.jpg"><script src="/task/js/ui/mode.js"></script><link rel="stylesheet" href="/task/css/prism.css" type="text/css"></head><body><script defer src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script defer src="/task/js/ui/fireworks.js"></script><canvas class="fireworks"></canvas><div class="container"><a class="sidebar-toggle hty-icon-button" id="menu-btn"><div class="hamburger hamburger--spin" type="button"><span class="hamburger-box"><span class="hamburger-inner"></span></span></div></a><div class="sidebar-toggle sidebar-overlay"></div><aside class="sidebar"><script src="/task/js/sidebar.js"></script><ul class="sidebar-nav"><li class="sidebar-nav-item sidebar-nav-toc hty-icon-button sidebar-nav-active" data-target="post-toc-wrap" title="文章目录"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-list-ordered"></use></svg></li><li class="sidebar-nav-item sidebar-nav-overview hty-icon-button" data-target="site-overview-wrap" title="站点概览"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-passport-line"></use></svg></li></ul><div class="sidebar-panel" id="site-overview-wrap"><div class="site-info fix-top"><a class="site-author-avatar" href="/task/about/" title="爱肖彤真是太好了"><img width="96" loading="lazy" src="/task/Yun.png" alt="爱肖彤真是太好了"></a><div class="site-author-name"><a href="/task/about/">爱肖彤真是太好了</a></div><a class="site-name" href="/task/about/site.html">工藤旧二の博客</a><sub class="site-subtitle"></sub><div class="site-desciption"></div></div><nav class="site-state"><a class="site-state-item hty-icon-button icon-home" href="/task/" title="首页"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-home-4-line"></use></svg></span></a><div class="site-state-item"><a href="/task/archives/" title="归档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-archive-line"></use></svg></span><span class="site-state-item-count">57</span></a></div><div class="site-state-item"><a href="/task/categories/" title="分类"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-2-line"></use></svg></span><span class="site-state-item-count">2</span></a></div><div class="site-state-item"><a href="/task/tags/" title="标签"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="site-state-item-count">50</span></a></div><a class="site-state-item hty-icon-button" target="_blank" rel="noopener" href="https://yun.yunyoujun.cn" title="文档"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-settings-line"></use></svg></span></a></nav><hr style="margin-bottom:0.5rem"><div class="links-of-author"><a class="links-of-author-item hty-icon-button" rel="noopener" title="RSS" target="_blank" style="color:orange"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-rss-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="QQ" target="_blank" style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="GitHub" target="_blank" style="color:#6e5494"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-github-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="微博" target="_blank" style="color:#E6162D"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-weibo-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="豆瓣" target="_blank" style="color:#007722"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-douban-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://music.163.com/#/user/home?id=552858287" title="网易云音乐" target="_blank" style="color:#C20C0C"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-netease-cloud-music-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="知乎" target="_blank" style="color:#0084FF"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-zhihu-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" href="https://space.bilibili.com/15081363" title="哔哩哔哩" target="_blank" style="color:#FF8EB3"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-bilibili-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Twitter" target="_blank" style="color:#1da1f2"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-twitter-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Telegram Channel" target="_blank" style="color:#0088CC"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-telegram-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="E-Mail" target="_blank" style="color:#8E71C1"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-mail-line"></use></svg></a><a class="links-of-author-item hty-icon-button" rel="noopener" title="Travelling" target="_blank" style="color:var(--hty-text-color)"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-send-plane-2-line"></use></svg></a></div><hr style="margin:0.5rem 1rem"><div class="links"><a class="links-item hty-icon-button" href="/task/links/" title="我的小伙伴们" style="color:dodgerblue"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-genderless-line"></use></svg></a></div><br><a class="links-item hty-icon-button" id="toggle-mode-btn" href="javascript:;" title="Mode" style="color: #f1cb64"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-contrast-2-line"></use></svg></a></div><div class="sidebar-panel sidebar-panel-active" id="post-toc-wrap"><div class="post-toc"><div class="post-toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81celery%E4%BB%8B%E7%BB%8D"><span class="toc-text">一、celery介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81celery%E6%9E%B6%E6%9E%84"><span class="toc-text">二、celery架构</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E4%B8%AD%E9%97%B4%E4%BB%B6"><span class="toc-text">消息中间件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E6%89%A7%E8%A1%8C%E5%8D%95%E5%85%83"><span class="toc-text">任务执行单元</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E7%BB%93%E6%9E%9C%E5%AD%98%E5%82%A8"><span class="toc-text">任务结果存储</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-text">使用场景</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81celery%E4%BD%BF%E7%94%A8"><span class="toc-text">三、celery使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%B8%80%EF%BC%9A%E5%9C%A8%E4%B8%80%E4%B8%AA%E6%96%87%E4%BB%B6%E5%A4%B9%E5%86%85%E7%9A%84%E4%B8%89%E4%B8%AA%E9%A1%B5%E9%9D%A2"><span class="toc-text">方式一：在一个文件夹内的三个页面</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E5%BC%8F%E4%BA%8C%EF%BC%9Aworker%E5%8D%95%E7%8B%AC%E5%81%9A%E4%B8%80%E4%B8%AA%E9%A1%B9%E7%9B%AE%E6%96%87%E4%BB%B6%EF%BC%8C%E6%B7%BB%E5%8A%A0%E4%BB%BB%E5%8A%A1%E5%92%8C%E8%8E%B7%E5%8F%96%E7%BB%93%E6%9E%9C%E5%88%86%E7%A6%BB%E5%87%BA%E6%9D%A5%EF%BC%88%E6%89%A7%E8%A1%8C%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%EF%BC%89"><span class="toc-text">方式二：worker单独做一个项目文件，添加任务和获取结果分离出来（执行异步任务）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%A7%E8%A1%8C%E5%BB%B6%E8%BF%9F%E4%BB%BB%E5%8A%A1"><span class="toc-text">执行延迟任务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1"><span class="toc-text">添加定时任务</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%9B%E3%80%81django%E4%B8%AD%E9%85%8D%E7%BD%AEcelery"><span class="toc-text">四、django中配置celery</span></a></li></ol></div></div></div></aside><main class="sidebar-translate" id="content"><div id="post"><article class="post-block" itemscope itemtype="https://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://huang_zhao.gitee.io/task/task/2020/04/25/python/%E6%A8%A1%E5%9D%97/celery/"><span hidden itemprop="author" itemscope itemtype="https://schema.org/Person"><meta itemprop="name" content="爱肖彤真是太好了"><meta itemprop="description"></span><span hidden itemprop="publisher" itemscope itemtype="https://schema.org/Organization"><meta itemprop="name" content="工藤旧二の博客"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">celery基础</h1><div class="post-meta"><div class="post-time" style="display:block"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-line"></use></svg></span> <time title="创建时间：2020-04-25 13:20:11" itemprop="dateCreated datePublished" datetime="2020-04-25T13:20:11+08:00">2020-04-25</time><span class="post-meta-divider">-</span><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-calendar-2-line"></use></svg></span> <time title="修改时间：2020-08-26 09:02:17" itemprop="dateModified" datetime="2020-08-26T09:02:17+08:00">2020-08-26</time></div><span class="post-count"><span class="post-symbolcount"><span class="post-meta-item-icon" title="本文字数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-file-word-line"></use></svg></span> <span title="本文字数">1.3k</span><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读时长"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-timer-line"></use></svg></span> <span title="阅读时长">5m</span></span></span><span class="post-busuanzi"><span class="post-meta-divider">-</span><span class="post-meta-item-icon" title="阅读次数"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg> <span id="busuanzi_value_page_pv"></span></span></span><div class="post-classify"><span class="post-category"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-folder-line"></use></svg></span> <span itemprop="about" itemscope itemtype="https://schema.org/Thing"><a class="category" href="/task/categories/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/" style="--text-color:var(--hty-text-color)" itemprop="url" rel="index"><span itemprop="text">技术分享</span></a></span></span><span class="post-tag"><span class="post-meta-divider">-</span><a class="tag" href="/task/tags/django/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">django</span></a><a class="tag" href="/task/tags/python/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">python</span></a><a class="tag" href="/task/tags/%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1%E6%A1%86%E6%9E%B6/" style="--text-color:var(--hty-text-color)"><span class="post-meta-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-price-tag-3-line"></use></svg></span><span class="tag-name">异步任务框架</span></a></span></div></div></header><section class="post-body" itemprop="articleBody"><div class="post-content markdown-body" style="--smc-primary:#0078E7;"><h2 id="一、celery介绍"><a href="#一、celery介绍" class="headerlink" title="一、celery介绍"></a>一、celery介绍</h2><p>Celery 官网：<a target="_blank" rel="noopener" href="http://www.celeryproject.org/">http://www.celeryproject.org/</a></p>
<p>Celery 官方文档英文版：<a target="_blank" rel="noopener" href="http://docs.celeryproject.org/en/latest/index.html">http://docs.celeryproject.org/en/latest/index.html</a></p>
<p>Celery 官方文档中文版：<a target="_blank" rel="noopener" href="http://docs.jinkan.org/docs/celery/">http://docs.jinkan.org/docs/celery/</a></p>
<ul>
<li>异步任务框架</li>
<li>执行异步任务</li>
<li>执行延迟任务</li>
<li>执行定时任务</li>
</ul>
<h2 id="二、celery架构"><a href="#二、celery架构" class="headerlink" title="二、celery架构"></a>二、celery架构</h2><p><img src="https://gitee.com/huang_zhao/hz/raw/master/hz/007S8ZIlgy1ggqw0f7bltj30q80c174d.jpg" alt="007S8ZIlgy1ggqw0f7bltj30q80c174d" loading="lazy"></p>
<h4 id="消息中间件"><a href="#消息中间件" class="headerlink" title="消息中间件"></a>消息中间件</h4><p>Celery本身不提供消息服务，但是可以方便的和第三方提供的消息中间件集成。包括，RabbitMQ, Redis等等</p>
<h4 id="任务执行单元"><a href="#任务执行单元" class="headerlink" title="任务执行单元"></a>任务执行单元</h4><p>Worker是Celery提供的任务执行的单元，worker并发的运行在分布式的系统节点中。</p>
<h4 id="任务结果存储"><a href="#任务结果存储" class="headerlink" title="任务结果存储"></a>任务结果存储</h4><p>Task result store用来存储Worker执行的任务的结果，Celery支持以不同方式存储任务的结果，包括AMQP, redis等</p>
<h4 id="使用场景"><a href="#使用场景" class="headerlink" title="使用场景"></a>使用场景</h4><p>异步执行：解决耗时任务,将耗时操作任务提交给Celery去异步执行，比如发送短信/邮件、消息推送、音视频处理等等</p>
<p>延迟执行：解决延迟任务</p>
<p>定时执行：解决周期(周期)任务,比如每天数据统计</p>
<h2 id="三、celery使用"><a href="#三、celery使用" class="headerlink" title="三、celery使用"></a>三、celery使用</h2><p>安装：<code>pip install celery</code></p>
<p>根据celery架构，我们可以看出，worker就像是一个工人，一直在工作，如果有工作的时候就会从broker（Redist）里去拿过来执行，并放到pakend（redist）中存放结果</p>
<h3 id="方式一：在一个文件夹内的三个页面"><a href="#方式一：在一个文件夹内的三个页面" class="headerlink" title="方式一：在一个文件夹内的三个页面"></a>方式一：在一个文件夹内的三个页面</h3><p><strong>worker执行页面</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> celery

<span class="token comment" spellcheck="true"># broker存储的位置</span>
broker <span class="token operator">=</span> <span class="token string">'redis://127.0.0.1:6379/1'</span>
<span class="token comment" spellcheck="true"># backend存储的位置</span>
backend <span class="token operator">=</span><span class="token string">'redis://127.0.0.1:6379/2'</span>

<span class="token comment" spellcheck="true"># 实例化的celery对象</span>
app<span class="token operator">=</span>celery<span class="token punctuation">.</span>Celery<span class="token punctuation">(</span>__name__<span class="token punctuation">,</span>broker<span class="token operator">=</span>broker<span class="token punctuation">,</span>backend<span class="token operator">=</span>backend<span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 需要添加的任务</span>
@app<span class="token punctuation">.</span>task
<span class="token keyword">def</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token operator">*</span>y<span class="token punctuation">)</span>
    <span class="token keyword">return</span> x<span class="token operator">+</span>y</code></pre>
<p><strong>broker提交任务的页面</strong></p>
<pre class=" language-python"><code class="language-python">
<span class="token keyword">from</span> celery_test <span class="token keyword">import</span> add
<span class="token comment" spellcheck="true"># 执行这个文件，就是把这个任务添加到数据库中，只要worker在工作</span>
<span class="token comment" spellcheck="true"># 就会把这个任务从数据库1中拿出来执行，并把结果放到数据库2中</span>
ret <span class="token operator">=</span> add<span class="token punctuation">.</span>delay<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span>
<span class="token comment" spellcheck="true"># ret 是这个任务的uuid，用于获取任务结果</span></code></pre>
<p><strong>backend获取任务结果的页面</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">from</span> celery_test <span class="token keyword">import</span> app

<span class="token keyword">from</span> celery<span class="token punctuation">.</span>result <span class="token keyword">import</span> AsyncResult
<span class="token comment" spellcheck="true"># 任务对象的唯一标识：uuid</span>
id <span class="token operator">=</span> <span class="token string">'19dc2faa-39f9-47b6-af77-e9d3a4d05d2e'</span>
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    async1 <span class="token operator">=</span> AsyncResult<span class="token punctuation">(</span>id<span class="token operator">=</span>id<span class="token punctuation">,</span> app<span class="token operator">=</span>app<span class="token punctuation">)</span>
    <span class="token keyword">if</span> async1<span class="token punctuation">.</span>successful<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        result <span class="token operator">=</span> async1<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
    <span class="token keyword">elif</span> async1<span class="token punctuation">.</span>failed<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'任务失败'</span><span class="token punctuation">)</span>
    <span class="token keyword">elif</span> async1<span class="token punctuation">.</span>status <span class="token operator">==</span> <span class="token string">'PENDING'</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'任务等待中被执行'</span><span class="token punctuation">)</span>
    <span class="token keyword">elif</span> async1<span class="token punctuation">.</span>status <span class="token operator">==</span> <span class="token string">'RETRY'</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'任务异常后正在重试'</span><span class="token punctuation">)</span>
    <span class="token keyword">elif</span> async1<span class="token punctuation">.</span>status <span class="token operator">==</span> <span class="token string">'STARTED'</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'任务已经开始被执行'</span><span class="token punctuation">)</span></code></pre>
<h3 id="方式二：worker单独做一个项目文件，添加任务和获取结果分离出来（执行异步任务）"><a href="#方式二：worker单独做一个项目文件，添加任务和获取结果分离出来（执行异步任务）" class="headerlink" title="方式二：worker单独做一个项目文件，添加任务和获取结果分离出来（执行异步任务）"></a>方式二：worker单独做一个项目文件，添加任务和获取结果分离出来（执行异步任务）</h3><p>创建一个celery项目（包），内部必须含有名字为celery的py文件，在内部创建celery对象</p>
<p><strong>celery.py</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> celery


broker <span class="token operator">=</span> <span class="token string">'redis://127.0.0.1:6379/1'</span>
backend <span class="token operator">=</span><span class="token string">'redis://127.0.0.1:6379/2'</span>

app<span class="token operator">=</span>celery<span class="token punctuation">.</span>Celery<span class="token punctuation">(</span>__name__<span class="token punctuation">,</span>broker<span class="token operator">=</span>broker<span class="token punctuation">,</span>backend<span class="token operator">=</span>backend<span class="token punctuation">,</span>include<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'celery_task.task1'</span><span class="token punctuation">,</span><span class="token string">'celery_task.task2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre>
<p>编写需要添加的任务也在这个包内，可创建不同的任务文件，可添加多个</p>
<p><strong>task1.py</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">from</span> <span class="token punctuation">.</span>celery <span class="token keyword">import</span> app

@app<span class="token punctuation">.</span>task
<span class="token keyword">def</span> <span class="token function">add</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span>y<span class="token punctuation">)</span>
    <span class="token keyword">return</span> x<span class="token operator">+</span>y</code></pre>
<h3 id="执行延迟任务"><a href="#执行延迟任务" class="headerlink" title="执行延迟任务"></a>执行延迟任务</h3><p>添加任务页面</p>
<pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 执行延迟任务就是多个一个时间参数</span>
<span class="token comment" spellcheck="true"># 这里注意，时间参数是根据utc时间，并不是中国时间</span>

<span class="token keyword">from</span> datetime <span class="token keyword">import</span> datetime<span class="token punctuation">,</span> timedelta
<span class="token comment" spellcheck="true"># 时间对象必须和时间对象相加</span>
eta<span class="token operator">=</span>datetime<span class="token punctuation">.</span>utcnow<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> timedelta<span class="token punctuation">(</span>seconds<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">)</span>
add<span class="token punctuation">.</span>apply_async<span class="token punctuation">(</span>args<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">,</span> eta<span class="token operator">=</span>eta<span class="token punctuation">)</span>
</code></pre>
<h3 id="添加定时任务"><a href="#添加定时任务" class="headerlink" title="添加定时任务"></a>添加定时任务</h3><p>celery页面</p>
<pre class=" language-python"><code class="language-python"><span class="token comment" spellcheck="true"># 时区</span>
app<span class="token punctuation">.</span>conf<span class="token punctuation">.</span>timezone <span class="token operator">=</span> <span class="token string">'Asia/Shanghai'</span>
<span class="token comment" spellcheck="true"># 是否使用UTC</span>
app<span class="token punctuation">.</span>conf<span class="token punctuation">.</span>enable_utc <span class="token operator">=</span> <span class="token boolean">False</span>

<span class="token comment" spellcheck="true"># 任务的定时配置</span>
<span class="token keyword">from</span> datetime <span class="token keyword">import</span> timedelta
<span class="token keyword">from</span> celery<span class="token punctuation">.</span>schedules <span class="token keyword">import</span> crontab
app<span class="token punctuation">.</span>conf<span class="token punctuation">.</span>beat_schedule <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">'low-task'</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token string">'task'</span><span class="token punctuation">:</span> <span class="token string">'celery_task.tasks.low'</span><span class="token punctuation">,</span>
        <span class="token string">'schedule'</span><span class="token punctuation">:</span> timedelta<span class="token punctuation">(</span>seconds<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token comment" spellcheck="true"># 'schedule': crontab(hour=8, day_of_week=1),  # 每周一早八点</span>
        <span class="token string">'args'</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">150</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true"># 定时任务的添加必须要新启动一个beat命令去工作</span>
<span class="token comment" spellcheck="true"># celery beat -A pc_celery -l info</span>
<span class="token comment" spellcheck="true"># celery -A pc_celery worker -l info -P eventlet</span></code></pre>
<h2 id="四、django中配置celery"><a href="#四、django中配置celery" class="headerlink" title="四、django中配置celery"></a>四、django中配置celery</h2><p>celery包最好放在根路径下</p>
<p><strong>添加定时任务</strong></p>
<p>celery.py</p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">import</span> os

<span class="token comment" spellcheck="true"># 配置django配置</span>
os<span class="token punctuation">.</span>environ<span class="token punctuation">.</span>setdefault<span class="token punctuation">(</span><span class="token string">'DJANGO_SETTINGS_MODULE'</span><span class="token punctuation">,</span> <span class="token string">'luffyapi.settings.dev'</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 配置celery的worker环境</span>
<span class="token keyword">from</span> celery <span class="token keyword">import</span> Celery
broker <span class="token operator">=</span> <span class="token string">'redis://127.0.0.1:6379/1'</span>
backend <span class="token operator">=</span> <span class="token string">'redis://127.0.0.1:6379/2'</span>

<span class="token comment" spellcheck="true"># 实例化worker对象app，用include添加定时任务</span>
app<span class="token operator">=</span>Celery<span class="token punctuation">(</span>__name__<span class="token punctuation">,</span>broker<span class="token operator">=</span>broker<span class="token punctuation">,</span>backend<span class="token operator">=</span>backend<span class="token punctuation">,</span>include<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'celery_task.task2'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

<span class="token comment" spellcheck="true"># 时区</span>
app<span class="token punctuation">.</span>conf<span class="token punctuation">.</span>timezone <span class="token operator">=</span> <span class="token string">'Asia/Shanghai'</span>
<span class="token comment" spellcheck="true"># 是否使用UTC</span>
app<span class="token punctuation">.</span>conf<span class="token punctuation">.</span>enable_utc <span class="token operator">=</span> <span class="token boolean">False</span>

<span class="token comment" spellcheck="true"># 任务的定时配置</span>
<span class="token keyword">from</span> datetime <span class="token keyword">import</span> timedelta
<span class="token keyword">from</span> celery<span class="token punctuation">.</span>schedules <span class="token keyword">import</span> crontab
app<span class="token punctuation">.</span>conf<span class="token punctuation">.</span>beat_schedule <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">'low-task'</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
        <span class="token string">'task'</span><span class="token punctuation">:</span> <span class="token string">'celery_task.task2.update_banner'</span><span class="token punctuation">,</span>
        <span class="token string">'schedule'</span><span class="token punctuation">:</span> timedelta<span class="token punctuation">(</span>seconds<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token comment" spellcheck="true"># 'schedule': crontab(hour=8, day_of_week=1),  # 每周一早八点</span>
        <span class="token comment" spellcheck="true"># 'args': (),</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true"># 定时任务的添加必须要新启动一个beat命令去工作</span>
<span class="token comment" spellcheck="true"># celery beat -A celery_task -l info</span></code></pre>
<p><strong>task定时任务</strong></p>
<pre class=" language-python"><code class="language-python"><span class="token keyword">from</span> <span class="token punctuation">.</span>celery <span class="token keyword">import</span> app
<span class="token keyword">from</span> django<span class="token punctuation">.</span>core<span class="token punctuation">.</span>cache <span class="token keyword">import</span> cache
<span class="token comment" spellcheck="true"># 每三秒跟新一次缓存</span>
<span class="token comment" spellcheck="true"># 添加一个任务，celery内设定间隔时间3秒</span>
<span class="token comment" spellcheck="true"># 任务内去cache跟新banner_list，从数据库中拿到banner放到cache中的banner_list中</span>

<span class="token keyword">from</span> home<span class="token punctuation">.</span>models <span class="token keyword">import</span> Banner

<span class="token comment" spellcheck="true"># from luffyapi.settings import const</span>

<span class="token comment" spellcheck="true"># [OrderedDict(</span>
<span class="token comment" spellcheck="true">#     [('image', 'http://127.0.0.1:8000/media/banner/banner1.png'), ('link', '/free-courses'), ('name', 'banner1')]),</span>
<span class="token comment" spellcheck="true">#  OrderedDict(</span>
<span class="token comment" spellcheck="true">#      [('image', 'http://127.0.0.1:8000/media/banner/banner2.png'), ('link', '/light-courses'), ('name', 'banner2')]),</span>
<span class="token comment" spellcheck="true">#  OrderedDict(</span>
<span class="token comment" spellcheck="true">#      [('image', 'http://127.0.0.1:8000/media/banner/banner3.png'), ('link', '/actual-courses'), ('name', 'banner3')])]</span>
<span class="token keyword">from</span> home<span class="token punctuation">.</span>sers <span class="token keyword">import</span> BannerSer

@app<span class="token punctuation">.</span>task
<span class="token keyword">def</span> <span class="token function">update_banner</span><span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span><span class="token punctuation">:</span>
    queryset <span class="token operator">=</span> Banner<span class="token punctuation">.</span>objects<span class="token punctuation">.</span>filter<span class="token punctuation">(</span>is_delete<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
    banner_ser <span class="token operator">=</span> BannerSer<span class="token punctuation">(</span>queryset<span class="token punctuation">,</span>many<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span>

    <span class="token keyword">for</span> banner <span class="token keyword">in</span> banner_ser<span class="token punctuation">.</span>data<span class="token punctuation">:</span>
        banner<span class="token punctuation">[</span><span class="token string">'image'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'http://127.0.0.1:8000'</span> <span class="token operator">+</span> banner<span class="token punctuation">[</span><span class="token string">'image'</span><span class="token punctuation">]</span>
    cache<span class="token punctuation">.</span>set<span class="token punctuation">(</span><span class="token string">'banner_list'</span><span class="token punctuation">,</span> banner_ser<span class="token punctuation">.</span>data<span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>banner_ser<span class="token punctuation">.</span>data<span class="token punctuation">)</span>
    <span class="token keyword">return</span> <span class="token string">'更新banner成功'</span>
</code></pre>
</div><div id="reward-container"><span class="hty-icon-button button-glow" id="reward-button" title="打赏" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === &quot;none&quot;) ? &quot;block&quot; : &quot;none&quot;;"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-hand-coin-line"></use></svg></span><div id="reward-comment">点我就给你看点有意思的</div><div id="qr" style="display:none;"><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/alipay.png" alt="支付宝" title="支付宝"></a><div><span style="color:#00A3EE"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-alipay-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/qqpay.jpg" alt="QQ 支付" title="QQ 支付"></a><div><span style="color:#12B7F5"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-qq-line"></use></svg></span></div></div><div style="display:inline-block"><a target="_blank" rel="noopener" href="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg"><img loading="lazy" src="https://gitee.com/huang_zhao/hz/raw/master/hz/wxpay.jpg" alt="微信支付" title="微信支付"></a><div><span style="color:#2DC100"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-wechat-pay-line"></use></svg></span></div></div></div></div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong>爱肖彤真是太好了</li><li class="post-copyright-link"><strong>本文链接：</strong><a href="http://huang_zhao.gitee.io/task/2020/04/25/python/%E6%A8%A1%E5%9D%97/celery/" title="celery基础">http://huang_zhao.gitee.io/task/2020/04/25/python/%E6%A8%A1%E5%9D%97/celery/</a></li><li class="post-copyright-license"><strong>版权声明：</strong>本博客所有文章除特别声明外，均默认采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="noopener" title="CC BY-NC-SA 4.0 "><svg class="icon"><use xlink:href="#icon-creative-commons-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-by-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-nc-line"></use></svg><svg class="icon"><use xlink:href="#icon-creative-commons-sa-line"></use></svg></a> 许可协议。</li></ul></section></article><div class="post-nav"><div class="post-nav-item"><a class="post-nav-prev" href="/task/2020/04/25/python/%E6%A8%A1%E5%9D%97/%E6%94%AF%E4%BB%98%E5%AE%9D%E7%BD%91%E9%A1%B5%E6%94%AF%E4%BB%98%E6%8E%A5%E5%8F%A3%E8%B0%83%E7%94%A8/" rel="prev" title="支付宝网页支付接口调用"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-left-s-line"></use></svg><span class="post-nav-text">支付宝网页支付接口调用</span></a></div><div class="post-nav-item"><a class="post-nav-next" href="/task/2020/04/23/%E6%95%B0%E6%8D%AE%E5%BA%93/redis/Redis%E7%AE%A1%E9%81%93%EF%BC%8C%E4%BD%8D%E5%9B%BE%EF%BC%8Cgeo%EF%BC%8CHyperLogLog%EF%BC%8C%E5%8F%91%E5%B8%83%E8%AE%A2%E9%98%85/" rel="next" title="Redis管道，位图，geo，HyperLogLog，发布订阅"><span class="post-nav-text">Redis管道，位图，geo，HyperLogLog，发布订阅</span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-right-s-line"></use></svg></a></div></div></div></main><footer class="sidebar-translate" id="footer"><div class="copyright"><span>&copy; 2019 – 2022 </span><span class="with-love" id="animate"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-cloud-line"></use></svg></span><span class="author"> 爱肖彤真是太好了</span></div><div class="powered"><span>由 <a href="https://hexo.io" target="_blank" rel="noopener">Hexo</a> 驱动 v5.1.1</span><span class="footer-separator">|</span><span>主题 - <a rel="noopener" href="https://github.com/YunYouJun/hexo-theme-yun" target="_blank"><span>Yun</span></a> v0.9.7</span></div><div class="live_time"><span>本博客已萌萌哒地运行</span><span id="display_live_time"></span><span class="moe-text">(●'◡'●)</span><script>function blog_live_time() {
  window.setTimeout(blog_live_time, 1000);
  const start = new Date('2019-04-12T00:00:00');
  const now = new Date();
  const timeDiff = (now.getTime() - start.getTime());
  const msPerMinute = 60 * 1000;
  const msPerHour = 60 * msPerMinute;
  const msPerDay = 24 * msPerHour;
  const passDay = Math.floor(timeDiff / msPerDay);
  const passHour = Math.floor((timeDiff % msPerDay) / 60 / 60 / 1000);
  const passMinute = Math.floor((timeDiff % msPerHour) / 60 / 1000);
  const passSecond = Math.floor((timeDiff % msPerMinute) / 1000);
  display_live_time.innerHTML = " " + passDay + " 天 " + passHour + " 小时 " + passMinute + " 分 " + passSecond + " 秒";
}
blog_live_time();
</script></div><div id="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_site_uv" title="总访客量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-user-line"></use></svg></span><span id="busuanzi_value_site_uv"></span></span><span class="footer-separator">|</span><span id="busuanzi_container_site_pv" title="总访问量"><span><svg class="icon" aria-hidden="true"><use xlink:href="#icon-eye-line"></use></svg></span><span id="busuanzi_value_site_pv"></span></span></div></footer><a class="hty-icon-button" id="goUp" aria-label="back-to-top" href="#"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-arrow-up-s-line"></use></svg><svg class="progress-circle-container" viewBox="0 0 100 100"><circle class="progress-circle" id="progressCircle" cx="50" cy="50" r="48" fill="none" stroke="#0078E7" stroke-width="2" stroke-linecap="round"></circle></svg></a><a class="popup-trigger hty-icon-button icon-search" id="search" href="javascript:;" title="搜索"><span class="site-state-item-icon"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-search-line"></use></svg></span></a><script>window.addEventListener("DOMContentLoaded", () => {
  // Handle and trigger popup window
  document.querySelector(".popup-trigger").addEventListener("click", () => {
    document.querySelector(".popup").classList.add("show");
    setTimeout(() => {
      document.querySelector(".search-input").focus();
    }, 100);
  });

  // Monitor main search box
  const onPopupClose = () => {
    document.querySelector(".popup").classList.remove("show");
  };

  document.querySelector(".popup-btn-close").addEventListener("click", () => {
    onPopupClose();
  });

  window.addEventListener("keyup", event => {
    if (event.key === "Escape") {
      onPopupClose();
    }
  });
});
</script><script src="/task/js/search/local-search.js" defer></script><div class="popup search-popup"><div class="search-header"><span class="popup-btn-close close-icon hty-icon-button"><svg class="icon" aria-hidden="true"><use xlink:href="#icon-close-line"></use></svg></span></div><div class="search-input-container"><input class="search-input" id="local-search-input" type="text" placeholder="搜索..." value=""></div><div id="local-search-result"></div></div></div><script defer src="/task/js/utils.js"></script><script defer src="/task/js/hexo-theme-yun.js"></script></body></html>